<?php

namespace App\Observers;

use App\Jobs\TranslateSlug;
use App\Models\Topic;
use App\utils\TopicTranslater;
use Illuminate\Support\Facades\DB;

class TopicObserver
{
    //处理seo字段
    public function saving(Topic $topic){
        $topic->desc = content_seo($topic->content);
        $topic->content = clean($topic->content,'my_editor');
        //if(!$topic->slug){
        //同步调用
            //$topic->slug = app(TopicTranslater::class)->translate($topic->title);
        //}
    }

    public function saved(Topic $topic){
        if(!$topic->slug){
            //异步调用翻译接口
            dispatch(new TranslateSlug($topic));
        }
    }


    public function deleted(Topic $topic)
    {
        DB::table('replies')->where('topic_id',$topic->id)->delete();
    }

}
